package com.tanhua.server.controller;

import com.tanhua.server.service.CommentsService;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ck
 * @date 2021/11/15 13:03
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {


    @Autowired
    private CommentsService commentsService;
    /**
     * 发布评论
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map){
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentsService.saveComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询评论列表
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity commentList(String movementId,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = commentsService.findCommenetList(movementId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论点赞功能
     */
    @GetMapping("/{id}/like")
    public ResponseEntity commentLike(@PathVariable("id") String commentId){
        Integer count = commentsService.commentLikes(commentId);
        return ResponseEntity.ok(count);
    }

    /**
     * 评论取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity commentUnLike(@PathVariable("id") String commentId){
        Integer count = commentsService.commentUnLikes(commentId);
        return ResponseEntity.ok(count);
    }
}
